package com.icss.clothes.cotroller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.icss.clothes.entity.*;
import com.icss.clothes.mapper.ShopMapper;
import com.icss.clothes.rabbitmq.MQMessage;
import com.icss.clothes.rabbitmq.MQResult;
import com.icss.clothes.service.imp.*;
import com.icss.clothes.util.RandomUtil;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Result;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class ShopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private SizeService sizeService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private BuyService buyService;
    //预先将商品的数量存储在redis数据库中
    @RequestMapping("/disposeShopNumber")
    @ResponseBody
    public String disposeShopNumber(){
        //需要一个能够获取得到所有商品详情信息的业务逻辑
        List<Shopdetail> shopdetailList=shopService.selectAllByNumberAndCd_id();
        //对得到的商品详情列表进行循环遍历
        for (Shopdetail ac:shopdetailList){
            redisTemplate.opsForValue().set("ac:"+ac.getCd_id(),ac.getC_number());
        }
        return "数据处理成功";
    }
    //用于实现购物车勾选结算
    @RequestMapping("/toByShops")
    public ModelAndView toBuyShops(int [] car_id,double oprice,HttpSession session){
        ModelAndView modelAndView = new ModelAndView("checkout");
        User loginUser=(User)session.getAttribute("loginUser");
        //需要有一个生成订单的业务逻辑
        String orders_id = orderService.generateOrders(car_id,oprice,loginUser.getUser_id());
        Orders orders=orderService.findAllByOrder_id(orders_id);
        //需要一个能够通过指定的订单编号获取得到订单详情的业务逻辑
        List<Order_detail> orderDetails=orderService.findOrderDetailAllByOrder_id(orders_id);
        //需要一个能够通过指定的用户编号获取得到收货地址信息的业务逻辑
        List<Address> addressList=addressService.findAllByUser_id(loginUser.getUser_id());
        modelAndView.addObject("orders",orders);
        modelAndView.addObject("orderDetails",orderDetails);
        modelAndView.addObject("addressList",addressList);
        return modelAndView;
    }
    //用于实现购物的控制器方法
    @RequestMapping("/toByShop")
    @ResponseBody
    public MQResult<String> toByShop(int c_id,int size_id,int color_id,int od_number,HttpSession session){
        MQResult<String > mqResult=new MQResult<>();
        User loginUser=(User)session.getAttribute("loginUser");
        if (loginUser==null){
            mqResult.setMsg("请先登录!");
            return mqResult;
        }
        //获取用户编号
        int user_id=loginUser.getUser_id();
        //需要一个能够通过指定的商品编号、尺码编号、颜色编号查询得到商品详情信息的业务逻辑
        Shopdetail shopdetailList=shopDetailService.findAll(c_id,size_id,color_id);
        //获取当前要购买的商品详情编号
        int cd_id=shopdetailList.getCd_id();
        //从redis数据库中预减该演唱会详情编号下的库存,目的是为了验证库存是否充足
        Long shopNumber=redisTemplate.opsForValue().decrement("ac:"+cd_id,od_number);
        if (shopNumber<0){//证明库存不足
            //把库存恢复为原来的值
            redisTemplate.opsForValue().increment("ac:"+cd_id,od_number);
            mqResult.setMsg("库存不足，请选择其他商品");
            return mqResult;
        }//创建一个要传递的消息对象
        String order_id= RandomUtil.generateOid();
        MQMessage mqMessage=new MQMessage(order_id,c_id,cd_id,od_number,user_id,size_id,color_id);
        rabbitTemplate.convertAndSend("ticket_direct_exchange",
                "ticket-key",
                mqMessage);
        mqResult.setMsg("购买成功，请等待处理...");
        mqResult.setData(order_id);
        return mqResult;
    }
    //用于验证订单是否存储在数据库中的控制器方法
    @RequestMapping("/checkOrderDisposeSuccess")
    @ResponseBody
    public String checkOrderDisposeSuccess(String order_id){
        //需要一个能够通过指定的订单编号查询得到订单信息的业务逻辑
        Orders orders=orderService.findAllByOrder_id(order_id);
        if (orders!=null){
            return "SUCCESS";
        }else {
            return "FAIL";
        }
    }
    //用于跳转到结算页面的控制器
    @RequestMapping("/toCheckout")
    public ModelAndView toCheckout(String order_id,HttpSession session){
        ModelAndView modelAndView=new ModelAndView("checkout");
        User user=(User) session.getAttribute("loginUser");
        int user_id=user.getUser_id();
        //需要一个能够通过指定的订单编号获取得到订单信息的业务逻辑
        Orders orders=orderService.findAllByOrder_id(order_id);
        //需要一个能够通过指定的订单编号获取得到订单详情的业务逻辑
        List<Order_detail> orderDetails=orderService.findOrderDetailAllByOrder_id(order_id);
        //需要一个能够通过指定的用户编号获取得到收货地址信息的业务逻辑
        List<Address> addressList=addressService.findAllByUser_id(user_id);
        modelAndView.addObject("orders",orders);
        modelAndView.addObject("orderDetails",orderDetails);
        modelAndView.addObject("addressList",addressList);
        return modelAndView;
    }
    //跳转到商品页面的控制器
    @RequestMapping("/shop")
    public ModelAndView toShop(Shop shopSearch,@RequestParam(defaultValue = "1") Integer currentPage) {
        ModelAndView modelAndView=new ModelAndView("shop");
        PageHelper.startPage(currentPage,9);
        //需要一个能够获取得到所有满足过滤条件的商品列表的业务逻辑
        List<Shop> shopList=shopService.findAllSearch(shopSearch);
        PageInfo<Shop> pageInfo=new PageInfo<>(shopList);
        //将得到的分页结果传递到要跳转的资源上去
        modelAndView.addObject("page",pageInfo);
        modelAndView.addObject("shopList",shopList);
        //需要一个能够获取得到所有商品类别的业务逻辑
        List<Type> typeList=typeService.findAll();
        modelAndView.addObject("typeList",typeList);
        modelAndView.addObject("shopSearch",shopSearch);
        List<Shop> maxSaleList=shopService.findAllBySale_number();
        //传最高销量列表，用于商品页展示最高的销量
        modelAndView.addObject("maxSaleList",maxSaleList);
        return modelAndView;
    }
    //用于展现商品详情的页面的控制器方法
    @RequestMapping("/product")
    public ModelAndView toProduct(int c_id,HttpSession session){
        ModelAndView modelAndView=new ModelAndView("product");
        //通过指定的商品编号获取得到商品详情信息的业务逻辑
        Shop shop = shopService.findById(c_id);
        List<Shopdetail> shopdetailList=shopService.findAllByC_cid(c_id);
        //通过指定的商品编号获取得到商品颜色的业务逻辑
        List<Color> colorList=shopService.findAllColorByC_id(c_id);
        //通过指定的商品编号获取得到商品类别的业务逻辑
        List<Type> typeList=typeService.findAllTypeByC_id(c_id);
        //通过指定的商品编号获取得到商品尺码的业务逻辑
        List<Size> sizeList=sizeService.findAllSizeByC_id(c_id);
        modelAndView.addObject("shop",shop);
        modelAndView.addObject("sizeList",sizeList);
        modelAndView.addObject("typeList",typeList);
        modelAndView.addObject("colorList",colorList);
        modelAndView.addObject("shopetailList",shopdetailList);
        return modelAndView;
    }


    @RequestMapping("/getDetailsByColor")
    @ResponseBody
    public List<Shopdetail> getDetailsByColor(int c_id,int color_id){
        List<Shopdetail> shopdetailList=shopService.findByCidAndColorId(color_id,c_id);
        return shopdetailList;
    }
    @RequestMapping("/getDetailBySize")
    @ResponseBody
    public double getDetailBySize(int c_id,int size_id){
        return shopService.findByCidAndSizeId(c_id,size_id);
    }
}
